package com.ball.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ball.entity.MatchDetail;
import com.ball.entity.MatchInfo;
import com.ball.entity.MessageReply;
import com.ball.service.MatchDetailService;
import com.ball.service.MatchInfoService;

@Controller
@RequestMapping("/match")
public class MatchController {
	@Resource
	private MatchInfoService matchInfoService;
	@Resource
	private MatchDetailService matchDetailService;

	@RequestMapping("/matchListPage")
	public String matchListPage(Model model) {
		List<MatchInfo> matchInfoList = matchInfoService.getMatchList(null, 0, 10);
		model.addAttribute("matchInfoList", matchInfoList);
		return "match/matchList";
	}
	
	@RequestMapping("/matchDetailPage")
	public String matchDetailPage(Model model) {
		//List<MatchInfo> matchInfoList = matchInfoService.getMatchList(null, 0, 10);
		//model.addAttribute("matchInfoList", matchInfoList);
		return "match/matchDetail";
	}
	
	@RequestMapping("/matchCreatePage")
	public String matchCreatePage(Model model) {
		//List<MatchInfo> matchInfoList = matchInfoService.getMatchList(null, 0, 10);
		//model.addAttribute("matchInfoList", matchInfoList);
		return "match/matchCreate";
	}
	
	@RequestMapping("/matchCreate")
	@ResponseBody
	public MessageReply matchCreate(HttpServletRequest request) {
		String matchName = request.getParameter("matchName");
		String matchType = request.getParameter("matchType");
		String startTime = request.getParameter("startTime");
		if(startTime != null && startTime.length()>0){
			startTime = startTime.replace(":", "").replaceAll("-", "").replace(" ", "")+"00";
			if(startTime.length()>14){
				startTime = null;
			}
		}
		String endTime = request.getParameter("endTime");
		if(endTime != null && endTime.length()>0){
			endTime = endTime.replace(":", "").replaceAll("-", "").replace(" ", "")+"00";
			if(endTime.length()>14){
				endTime = null;
			}
		}
		String address = request.getParameter("address");
		//主办方
		String holder = request.getParameter("holder");
		//承办方
		String operator = request.getParameter("operator");
		MessageReply reply = new MessageReply();
		if(matchName == null||matchName.length()<3){
			reply.setStatus("failed");
			reply.setMessage("比赛名称不能为空且不能小于三个字长度");
			return reply;
		}
		if(!"01".equals(matchType)){
			reply.setStatus("failed");
			reply.setMessage("抱歉，目前只支持淘汰赛，敬请期待，谢谢！");
			return reply;
		}
		try {
			MatchInfo matchInfo = new MatchInfo();
			matchInfo.setMatchName(matchName);
			matchInfo.setMatchType(matchType);
			matchInfo.setMatchStartTime(startTime);
			matchInfo.setMatchEndTime(endTime);
			matchInfo.setMatchLogo("/match/images/match/world.png");//暂时先要默认logo
			matchInfo.setMatchLevel("1");//目前支持1级比赛 且必须包含
			matchInfo.setMatchCatalogFlag("0");//目前不支持多级比赛目录
			int matchId = matchInfoService.saveMatch(matchInfo);
			MatchDetail matchDetail = new MatchDetail();
			matchDetail.setMatchId(matchId);
			matchDetail.setHolder(holder);
			matchDetail.setMatchAddress(address);
			matchDetail.setOperator(operator);
			matchDetailService.saveMatchDetail(matchDetail);
		} catch (Exception e) {
			reply.setStatus("failed");
			reply.setMessage("sorry,保存比赛数据失败，请联系app开发者，谢谢！");
			return reply;
		}
		reply.setStatus("success");
		reply.setMessage("创建比赛成功");
		return reply;
	}
	

	@RequestMapping("/matchList")
	@ResponseBody
	public MessageReply matchInfoPage(HttpServletRequest request) {
		String start = request.getParameter("start");
		String limit = request.getParameter("limit");
		MessageReply reply = new MessageReply();
		try {
			int s = 0;
			int l = 10;
			if (start != null) {
				s = Integer.parseInt(start);
			}
			if (limit != null) {
				l = Integer.parseInt(limit);
			}
			List<MatchInfo> matchInfoList = matchInfoService.getMatchList(null, s, l);

			reply.setStatus("success");
			reply.setInfo(matchInfoList);
		} catch (Exception e) {
			// TODO: handle exception
		}

		return reply;

	}

}